-- global_curse_weaken
-- created by xinj
-- 法老王诅咒减少攻击

return {
    apply = function(source, target, skillId, round, para, extra)
        extra = extra or {};
        local lastRound = extra.lastRound;

        local effectRound = -1;
        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[3] > 0 then
            effectRound = round + para[3] + 1;
        end

        local prop = PropM.combine(source, "magic_last", skillId);
        effectRound = PropM.apply(prop, effectRound);
        prop = PropM.combine(source, "magic_last", "assist");
        effectRound = PropM.apply(prop, effectRound);

        -- 所有技能持续时间+
        prop = PropM.combine(source, "magic_last", "all");
        effectRound = PropM.apply(prop, effectRound);

        -- 魔力
        local magic = source:getMagic();

        -- 威力
        local force = para[2];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        --最终威力
        local force = math.modf(force * (addon_rate + 1000)/1000);

        -- 遍历所有的怪
        local v;
        local reduceHp;
        local currHp;
        for p = 1, GRID_SIZE do
            local grid = DungeonM.getGridByPos(p);
            local monster = grid.monster;

            repeat
                -- 是未死亡的怪物
                if  not grid:isOpened() or
                    not monster or
                    grid.monster:isDead() then    -- 特殊技能对BOSS无效
                    break;
                end

                -- 如果免疫了该技能
                if SkillM.immunitySkill(source, monster, skillId) then
                    -- 有些怪可能取不到动作，比如龙蛋
                    if SkillM.getSequence(monster) then
                        SkillM.getSequence(monster):immunity(source, monster, skillId);
                    end
                    break;
                end

                -- 如果是boss
                if monster:isBoss() then
                    force = math.modf(force / 2);
                end

                -- 被对方百分比削弱
                prop = PropM.combine(monster, "weak_enemy", "magic");
                magic = PropM.apply(prop, magic);

                -- 被对方绝对值削弱
                prop = PropM.combine(monster, "weak_enemy2", "magic");
                magic = PropM.apply(prop, magic);

                -- 计算效果
                v = 1 + math.modf(force * (magic + 3) / 1000);

                -- 概率降为1点
                prop = PropM.combine(source, "curse_one", 1);

                local vAttack = v;
                local seed = DungeonM.getRandSeed("global_curse_weaken");
                if not monster:isBoss() and seed % 1000 < PropM.apply(prop, 1) then
                    vAttack = monster:getAttack();
                end

                local condition = {
                    ["value"] = vAttack,
                    ["end_round"] = effectRound,
                };

                -- 天赋起效，降低血量
                prop = PropM.combine(source, "curse_reduce_hp", skillId);
                reduceHp = PropM.apply(prop, v);
                if reduceHp > 0 then
                    currHp = monster:getHp();
                    v = math.max(1, currHp - reduceHp);
                    monster:setRealHp(v);
                    -- 记录降低的生命值，清除时使用
                    condition["hp"] = currHp - v;
                end

                -- 附加状态
                CombatStatusM.applyStatus(monster, "curse_magic", condition);

                -- 增加命中动作
                if SkillM.getSequence(monster) == nil then
                    SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
                end

                SkillM.getSequence(monster):hit(source, monster, skillId);

            until true;
        end

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { arr[1], tonumber(arr[2]), tonumber(arr[3] or -1), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 魔力
        local magic = source:getMagic();

        local base = para[2];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        --最终威力
        local force = math.modf(base * (addon_rate + 1000) / 1000);

        local vbase = math.abs(1 + math.modf(base * (magic + 3) / 1000));
        local vforce = math.abs(1 + math.modf(force * (magic + 3) / 1000));
        local vaddon = vforce - vbase;
        if vaddon == 0 then
            desc = string.gsub(desc, "{global_curse_weaken}", vforce);
        else
            desc = string.gsub(desc, "{global_curse_weaken}", vbase .. "(+" .. vaddon .. ")");
        end

        local key = FormulaM.invoke("CALC_SKILL_APPEND_PROP", skillId);
        local prop = PropM.combine(source, key, skillId);
        local append = PropM.apply(prop, vforce);

        desc = string.gsub(desc, "{append}", append);

        -- 回合数的显示
        local baseRound = para[3];
        local round = para[3];
        -- 增加持续时间的效果
        prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        -- 概率降为1点
        prop = PropM.combine(source, "curse_one", 1);
        local one = PropM.apply(prop, 1);
        -- 概率转为百分比
        one = math.modf(one / 10);
        desc = string.gsub(desc, "{one}", one);

        return desc;
    end,
};
